/*
 * Created on Jul 17, 2004
 *
 */
package littlecyberwar.control;

import javax.swing.*;
import java.io.*;
import java.beans.XMLDecoder;
import ModuleBuilder.model.*;

/** Uses a SwingWorker to perform a time-consuming (and utterly fake) task. */

/* 
 * LongTask.java is used by:
 *   ProgressBarDemo.java
 *   ProgressBarDemo2.java
 *   ProgressMonitorDemo
 */
public class XMLLoaderTask {
    private int lengthOfTask;
    private int current = 0;
    private boolean done = false;
    private boolean canceled = false;
    private String statMessage;

	JFileChooser fc;
	File file;
	JLabel progText;
	ScenarioModel scenarioMod=null;
	JFrame frame;
	JFrame parent;

    public XMLLoaderTask(JFileChooser fcIn, File fileIn, JLabel progTextIn, JFrame frameIn, JFrame parentIn) {
        //Compute length of task...
        //In a real program, this would figure out
        //the number of bytes to read or whatever.
        lengthOfTask = 1000;
        fc = fcIn;
        file = fileIn;
		progText = progTextIn;
		frame = frameIn;
		parent = parentIn;
    }

    /**
     * Called from ProgressBarDemo to start the task.
     */
    public void go() {
        final SwingWorker worker = new SwingWorker() {
            public Object construct() {
                current = 0;
                done = false;
                canceled = false;
                statMessage = null;
                return new ActualTask();
            }
        };
        worker.start();
    }

    /**
     * Called from ProgressBarDemo to find out how much work needs
     * to be done.
     */
    public int getLengthOfTask() {
        return lengthOfTask;
    }

    /**
     * Called from ProgressBarDemo to find out how much has been done.
     */
    public int getCurrent() {
        return current;
    }

    public void stop() {
        canceled = true;
        statMessage = null;
    }

    /**
     * Called from ProgressBarDemo to find out if the task has completed.
     */
    public boolean isDone() {
        return done;
    }

    /**
     * Returns the most recent status message, or null
     * if there is no current status message.
     */
    public String getMessage() {
        return statMessage;
    }

    /**
     * The actual long running task.  This runs in a SwingWorker thread.
     */
    class ActualTask {
        ActualTask() {
            //Fake a long task,
            //making a random amount of progress every second.
            while (!canceled && !done) {
				file = fc.getSelectedFile();
				String fn = file.getPath();

				// now serializr the result
				FileInputStream fi=null;
				BufferedInputStream bi=null;
				ObjectInputStream oi = null;
				
				try {
					fi = new FileInputStream(file);
					bi = new BufferedInputStream(fi);
					//oi = new ObjectInputStream(bi);
					
					XMLDecoder d = new XMLDecoder(bi);
					scenarioMod = (ScenarioModel)d.readObject();			
					oi.close();
			
				} catch (Exception ex) {
					ex.printStackTrace();
					return;
				}
				// now we have the ScenarioModel in memory, populate the manager
				//
				progText.setText("Loading Scenario..");
				ScenarioManager.setupGameState(scenarioMod, progText);
				frame.hide();
		
				// now we ask the user what kinda game he wants to play
				Object[] options = {"Hot Seat", "PBEM",
									"Online"};
				int n = JOptionPane.showOptionDialog(parent,
					"Select the type of game to play",
					"Choose the game type",			
					JOptionPane.YES_NO_OPTION,
					JOptionPane.QUESTION_MESSAGE,
					null,     //don't use a custom Icon
					options,  //the titles of buttons
					options[0]); //default button title
		
				GameTypeHandler.startGame(n, parent);
				done = true;
            }
        }
    }
}

